/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.hk.shop.web.sys;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.hk.shop.entity.shop.HkShopCoupon;
import com.thinkgem.jeesite.modules.hk.shop.entity.sys.HkShopRecBurst;
import com.thinkgem.jeesite.modules.hk.shop.service.shop.HkShopCouponService;
import com.thinkgem.jeesite.modules.hk.shop.service.sys.HkShopRecBurstService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 乐购推荐爆款优惠券Controller
 * @author 李欣荣
 * @version 2018-09-29
 */
@Controller
@RequestMapping(value = "${adminPath}/shop/sys/hkShopRecBurst")
public class HkShopRecBurstController extends BaseController {

	@Autowired
	private HkShopRecBurstService hkShopRecBurstService;

	@Autowired
	private HkShopCouponService hkShopCouponService;
	
	@ModelAttribute
	public HkShopRecBurst get(@RequestParam(required=false) String id) {
		HkShopRecBurst entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = hkShopRecBurstService.get(id);
		}
		if (entity == null){
			entity = new HkShopRecBurst();
		}
		return entity;
	}
	
	@RequiresPermissions("shop:sys:hkShopRecBurst:view")
	@RequestMapping(value = {"list", ""})
	public String list(HkShopRecBurst hkShopRecBurst, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<HkShopRecBurst> page = hkShopRecBurstService.findPage(new Page<HkShopRecBurst>(request, response), hkShopRecBurst); 
		model.addAttribute("page", page);
		return "hk/shop/sys/hkShopRecBurstList";
	}

	@RequiresPermissions("shop:sys:hkShopRecBurst:view")
	@RequestMapping(value = "form")
	public String form(HkShopRecBurst hkShopRecBurst, Model model) {
		model.addAttribute("hkShopRecBurst", hkShopRecBurst);
		return "hk/shop/sys/hkShopRecBurstForm";
	}

	@RequiresPermissions("shop:sys:hkShopRecBurst:edit")
	@RequestMapping(value = "save")
	public String save(HkShopRecBurst hkShopRecBurst, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, hkShopRecBurst)){
			return form(hkShopRecBurst, model);
		}

		String shopPouponId = hkShopRecBurst.getCoupon().getId();
		if (StringUtils.isBlank(shopPouponId)){
			addMessage(redirectAttributes,"保存乐购推荐爆款优惠券成功，请联系管理员或稍后重试！");
			return "redirect:"+Global.getAdminPath()+"/shop/shop/hkShopLuckyBurstCoupon/?repage";
		}else{
			HkShopCoupon hkShopCoupon = hkShopCouponService.get(shopPouponId);
			hkShopRecBurst.setProductId(hkShopCoupon.getProduct().getId());    //商品ID
			hkShopRecBurst.setDiscount(hkShopCoupon.getDiscount());  //折扣
			hkShopRecBurstService.save(hkShopRecBurst);
			addMessage(redirectAttributes, "保存乐购推荐爆款优惠券成功");
			return "redirect:"+Global.getAdminPath()+"/shop/sys/hkShopRecBurst/?repage";
		}
	}
	
	@RequiresPermissions("shop:sys:hkShopRecBurst:edit")
	@RequestMapping(value = "delete")
	public String delete(HkShopRecBurst hkShopRecBurst, RedirectAttributes redirectAttributes) {
		hkShopRecBurstService.delete(hkShopRecBurst);
		addMessage(redirectAttributes, "删除乐购推荐爆款优惠券成功");
		return "redirect:"+Global.getAdminPath()+"/shop/sys/hkShopRecBurst/?repage";
	}

}